package com.vm.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.view.View.OnClickListener;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TabHost;

import com.vm.adapter.FragAdapter;
import com.vm.fragment.TopDayFragment;
import com.vm.fragment.TopMonthFragment;
import com.vm.fragment.TopWeekFragment;
import com.vm.myview.NoScrollViewPager;
import com.wwtscapp.apk.R;

public class TopActivity extends FragmentActivity {
	Button rbtn_month_ranking;
	Button rbtn_week_ranking;
	Button rbtn_day_ranking;
	TabHost tabHost;
	Button btn_search;
	ImageView imgbtn_search_back;
	private NoScrollViewPager vp;
	List<Fragment> fragments;
	private FragAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ranking);
		// tabHost = getTabHost();
		// int count = TabConst.ConValue.HotTabText.length;
		// for (int i = 0; i < count; i++) {
		// TabSpec tabspec = tabHost
		// .newTabSpec(
		// getResources().getString(ConValue.HotTabText[i]))
		// .setIndicator(
		// getResources().getString(ConValue.HotTabText[i]))
		// .setContent(getTabItemIntent(i));
		// tabHost.addTab(tabspec);
		// }
		// tabHost.setCurrentTab(0);
		init();
		iniChilds();

	}

	private void iniChilds() {
		fragments = new ArrayList<Fragment>();
		fragments.add(new TopDayFragment());
		fragments.add(new TopWeekFragment());
		fragments.add(new TopMonthFragment());
		vp = (NoScrollViewPager) findViewById(R.id.viewpager);
		adapter = new FragAdapter(getSupportFragmentManager(), fragments);
		vp.setAdapter(adapter);

		// 设置最大缓存数
		// 默认缓存2个页面,当出现第三个页面会删掉之前缓存中的一个.
		vp.setOffscreenPageLimit(3);
		vp.setCurrentItem(0);
	}

	public void init() {
		rbtn_month_ranking = (Button) findViewById(R.id.rbtn_month_ranking);
		rbtn_week_ranking = (Button) findViewById(R.id.rbtn_week_ranking);
		rbtn_day_ranking = (Button) findViewById(R.id.rbtn_day_ranking);
//		btn_search = (Button) findViewById(R.id.btn_search);
		imgbtn_search_back = (ImageView) findViewById(R.id.imgbtn_search_back);
		rbtn_month_ranking.setOnClickListener(btn_listener);
		rbtn_week_ranking.setOnClickListener(btn_listener);
		rbtn_day_ranking.setOnClickListener(btn_listener);
//		btn_search.setOnClickListener(searchClickListener);
		imgbtn_search_back.setOnClickListener(btn_listener);

	}

	public OnClickListener btn_listener = new OnClickListener() {

		public void onClick(View v) {
			if (v == rbtn_day_ranking) {
				vp.setCurrentItem(0);
			}
			if (v == rbtn_week_ranking) {
				vp.setCurrentItem(1);
			} else if (v == rbtn_month_ranking) {
				vp.setCurrentItem(2);
			} else if (v == imgbtn_search_back) {
				onBackPressed();
			}
		}

	};

	public OnClickListener searchClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(TopActivity.this, SearchActivity.class);
			startActivity(intent);
		}
	};

}